<?php


namespace App\Models;


class ImCommunicationLogs extends BaseModel
{
    //通讯记录表
    protected $table = 'im_communication_logs';

    //发起者
    public function initiator()
    {
        return $this->belongsTo(ImUsers::class, 'initiator', 'id')
            ->select(['id', 'nickname', 'phone', 'headimg']);
    }

    //接收者信息处理
    public static function dataProcess($data)
    {
        $ids = [];
        $data->map(function ($v) use (&$ids) {
            $ids = array_merge($ids, json_decode($v->recipient, true));
        });
        $ids = array_values(array_unique($ids));
        $recUsers = User::whereIn('id', $ids)
            ->select(['id', 'nickname', 'phone', 'headimg'])
            ->get();
        foreach ($data as $val) {
            $tmp = [];
            foreach ($recUsers as $vv) {
                if (in_array($vv->id, json_decode($val->recipient, true))) {
                    $tmp[] = $vv;
                }
            }
            $val->recipient = $tmp;
            $val->status_str = $val->statusStr;
        }
        return $data;
    }

    //音视频接听状态
    public function getStatusStrAttribute()
    {
        $msg = '';
        switch ($this->attributes['status']) {
            case '.startComm':
                $msg = "发起通话";
                break;
            case '.handleOnOtherDevice':
                $msg = "已在其他端处理";
                break;
            case '.busy':
                $msg = "对方忙";
                break;
            case '.refuse':
                $msg = "拒绝通话";
                break;
            case '.cancel':
                $msg = "您已取消呼叫";
                break;
            case '.remoteCancel':
                $msg = "通话已取消";
                break;
            case '.noResponse':
                $msg = "对方超时未响应";
                break;
            case '.remoteNoResponse':
                $msg = "未接听";
                break;
            case '.hangup':
                $msg = "通话已结束";
                break;
            default:
                break;
        }
        return $msg;
    }
}
